﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace LCrs.Web.personeinfo
{
    /// <summary>
    /// GetPL 的摘要说明
    /// </summary>
    public class GetPL : IHttpHandler, IRequiresSessionState
    {
        BLL.PersonListInfo bll = new BLL.PersonListInfo();
        public void ProcessRequest(HttpContext context)
        {
            if (Web.Admin.myinfo.isLogin(context))
            {
                string ac = "";
                if (null != context.Request.Form["action"])
                {//获取前台传来的值  
                    ac = context.Request.Form["action"].ToString().Trim();
                }
                else if (null != context.Request.QueryString["action"])
                {
                    ac = context.Request.QueryString["action"].ToString().Trim();
                }
                if (ac == "query")
                    Query(context);
                else if (ac == "add")
                    add(context);
                else if (ac == "modify")
                    modify(context);
                else if (ac == "delete")
                    delete(context);
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }

        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["plid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            int mid = 0;
            string uidstr = context.Request.Form["plid"];
            if (uidstr != "")
                mid = int.Parse(uidstr);

            int plno = int.Parse(context.Request.Form["plno"]);
            int plnameid = int.Parse(context.Request.Form["plnameid"]);
            string plname = context.Request.Form["plname"];

            DateTime pldate = DateTime.Parse(context.Request.Form["pldate"]);

            int copynum = int.Parse(context.Request.Form["copynum"]);

            int pagenum = int.Parse(context.Request.Form["pagenum"]);
            string plmark = context.Request.Form["plmark"];

            int pid = int.Parse(context.Request.Form["pid"]);
            Model.PersonListInfo model = new Model.PersonListInfo();
            model.plno = plno;
            model.plname = plname;
            model.pldate = pldate;
            model.copynum = copynum;
            model.pagenum = pagenum;
            model.plmark = plmark;
            model.pid = pid;
            model.plnameid = plnameid;
            model.plid = mid;
            if (bll.Update(model))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            int plno = int.Parse(context.Request.Form["plno"]);
            string plname = context.Request.Form["plname"];
            int plnameid = int.Parse(context.Request.Form["plnameid"]);
            DateTime pldate = DateTime.Parse(context.Request.Form["pldate"]);

            int copynum = int.Parse(context.Request.Form["copynum"]);

            int pagenum = int.Parse(context.Request.Form["pagenum"]);
            string plmark = context.Request.Form["plmark"];

            int pid = int.Parse(context.Request.QueryString["pid"]);
            Model.PersonListInfo model = new Model.PersonListInfo();
            model.plno = plno;
            model.plname = plname;
            model.pldate = pldate;
            model.copynum = copynum;
            model.pagenum = pagenum;
            model.plmark = plmark;
            model.pid = pid;
            model.plnameid = plnameid;
            if (bll.Add(model) > 0)
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }


        }
        public void Query(HttpContext context)
        {


            context.Response.ContentType = "text/plain";
            string uname, utruename, st;
            uname = utruename = st = "";
            int pid = 0;
            if (null != context.Request.Form["pid"])
            {
                pid = int.Parse(context.Request.Form["pid"].ToString().Trim());
            }
            //================================================================  
            //获取分页和排序信息：页大小，页码，排序方式，排序字段  
            int pageRows, page;
            pageRows = 10;
            page = 1;
            string order, sort, oderby; order = sort = oderby = "";
            if (null != context.Request.Form["rows"])
            {
                pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
            }
            if (null != context.Request.Form["page"])
            {
                page = int.Parse(context.Request.Form["page"].ToString().Trim());
            }
            if (null != context.Request.Form["sort"])
            {
                order = context.Request.Form["sort"].ToString().Trim();
            }
            if (null != context.Request.Form["order"])
            {
                sort = context.Request.Form["order"].ToString().Trim();
            }
            StringBuilder strWhere = new StringBuilder();
            
            strWhere.AppendFormat(" and pid='{0}'", pid);
            
            if (sort != "" && order != "")
            {
                oderby = order + " " + sort;
            }
            DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
            int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
            string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
            context.Response.Write(strJson);//返回给前台页面  
            context.Response.End();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}